package com.petrochina.cloudconfig.biz;


import com.petrochina.cloudconfig.domain.Env;
import com.petrochina.cloudconfig.domain.Pkg;
import com.petrochina.cloudconfig.mapper.EnvMapper;
import com.petrochina.cloudconfig.mapper.PkgMapper;
import com.petrochina.cloudconfig.util.EnvSettingUtil;
import com.petrochina.cloudconfig.util.EnvUtil;
import com.petrochina.cloudconfig.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;


@Service
public class EnvBiz {

    private String key = "pkgs";

    @Value("${my.env.path}")
    private String envRootPath;

    @Autowired
    private EnvMapper envMapper;

    @Autowired
    private PkgMapper pkgMapper;



    public List<Env> list(String keyWord) {

        List<Env> envs= envMapper.list(keyWord);
        for (int i = 0; i < envs.size(); i++) {
            Env env = EnvUtil.getEnv(envRootPath, envs.get(i).getEnvFullName());
            envs.set(i,env);

        }
        return envs;
    }



    public boolean delEnv(String envFullName) {
        boolean result = false;
        if(envMapper.delEnvByFullName(envFullName)){
            Env env = EnvUtil.getEnv(envFullName);
            //
            File envFile = EnvSettingUtil.getEnvFile(envRootPath,env.getPkgFullName(),env.getEnvName());
            if(envFile.exists()){
                envFile.delete();
            }
            result = true;
        }
        return result;

    }


    public void getEnv(HttpServletResponse response, String envFullName) {
        Env env = envMapper.getDev(envFullName);
        if(env!= null && env.getPkgFullName()!=null){
            env = EnvUtil.getEnv(envFullName);
            File envFile = EnvSettingUtil.getEnvFile(envRootPath,env.getPkgFullName(),env.getEnvName());
            FileUtil.saveFileToResponse(response,envFile);
        }
    }


    public Env getEnvInfo(String envFullName){
        Env env = envMapper.getDev(envFullName);
        if(env != null && env.getPkgFullName()!=null){
            env = EnvUtil.getEnv(envRootPath, env.getEnvFullName());
        }
        return env;
    }



    public boolean addEnv(String envFullName, MultipartFile file) {
        boolean result = false;
        Env env = EnvUtil.getEnv(envFullName);
        Pkg pkg = pkgMapper.getPkgByFullName(env.getPkgFullName());
        if(pkg.getPkgId()!=null){
            result = addEnvCore(env,file);
        }
        return result;

    }

    public boolean addEnv(int pkgId, String envName, MultipartFile file) {
        boolean result = false;
        Pkg pkg = pkgMapper.getPkg(pkgId);
        if(pkg.getPkgId()!=null){
            Env env = EnvUtil.getEnv(pkg.getPkgFullName()+"-"+envName);
            result = addEnvCore(env,file);
        }
        return result;
    }

    public  boolean addEnvCore(Env env,MultipartFile file){
        boolean result = false;
        if(envMapper.addEnv(env)){
            File envFile = EnvSettingUtil.getEnvFile(envRootPath,  env.getPkgFullName(), env.getEnvName());
            if(!envFile.exists()){
                envFile.getParentFile().mkdirs();
            }
            try {
                file.transferTo(envFile);
            } catch (IOException e) {
                e.printStackTrace();
                return  result;
            }
            result = true;
        }
        return result;
    }

    public List<Env> listByPkgId(int pkgId) {
        Pkg pkg = pkgMapper.getPkg(pkgId);
        if(pkg.getPkgId()!=null){
            return envMapper.list(pkg.getPkgFullName());
        }else{
            return null;
        }

    }


    public boolean delEnv(int pkgId, String envName) {
        boolean result = false;
        Pkg pkg = pkgMapper.getPkg(pkgId);
        if(pkg.getPkgId()!=null){
            if(envMapper.delEnv(pkg.getPkgFullName(),envName)){
                Env env = EnvUtil.getEnv(pkg.getPkgFullName(),envName);
                //
                File envFile = EnvSettingUtil.getEnvFile(envRootPath,env.getPkgFullName(),env.getEnvName());
                if(envFile.exists()){
                    envFile.delete();
                }
                result = true;
            }
        }

        return result;
    }


}
